package com.caroil.caroilservice.activitys.login.fragment;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import com.caroil.caroilservice.R;
import com.caroil.caroilservice.base.BaseFragment;
import com.caroil.caroilservice.view.TitleControlView;
import butterknife.BindView;

/**
 * 隐私协议
 */
public class AgreementWebFragment extends BaseFragment {

    public static AgreementWebFragment newInstance() {
        AgreementWebFragment agreementWebFragment = new AgreementWebFragment();
        return agreementWebFragment;
    }

    @BindView(R.id.webView)
    WebView webView;
    @BindView(R.id.progressBar1)
    ProgressBar pg1;
    public String Url = "https://www.baidu.com/";
    private TitleControlView t;

    @Override
    protected int initLayout() {
        return R.layout.ac_onekyoil;
    }

    @Override
    protected void initView(View view) {
        initTitle(view);
        setBarState(false);
        //支持javascript
        webView.getSettings().setJavaScriptEnabled(true);
        // 设置可以支持缩放
        webView.getSettings().setSupportZoom(false);
        // 设置出现缩放工具
        webView.getSettings().setBuiltInZoomControls(false);
        //扩大比例的缩放
        webView.getSettings().setUseWideViewPort(false);
        //自适应屏幕
        webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webView.getSettings().setLoadWithOverviewMode(true);

        //如果不设置WebViewClient，请求会跳转系统浏览器
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                String url = request.getUrl().toString();
                if (url == null) return false;
                try {
                    if (url.startsWith("weixin://") || url.startsWith("alipays://") ||
                            url.startsWith("mailto://") || url.startsWith("tel://")
                        //其他自定义的scheme
                    ) {
                        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                        startActivity(intent);
                        return true;
                    }
                } catch (Exception e) { //防止crash (如果手机上没有安装处理某个scheme开头的url的APP, 会导致crash)
                    return false;
                }

                //处理http和https开头的url
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                if (t != null)
                    t.setTitle(view.getTitle() + "");
            }
        });
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                // TODO 自动生成的方法存根
                //
                if (pg1 == null) {
                    return;
                }
                if (newProgress == 100) {
                    pg1.setVisibility(View.GONE);
                } else {
                    pg1.setVisibility(View.VISIBLE);//开始加载网页时显示进度条
                    pg1.setProgress(newProgress);//设置进度值
                }
            }

        });
        webView.loadUrl(Url);
    }

    @Override
    protected void initData(Context mContext) {

    }

    @Override
    protected void loadMethod() {

    }



    /**
     * 设置标题栏
     */
    private void initTitle(View view) {
        t = new TitleControlView(view, () -> goBack(getActivity()));
        t.setTitle("");
    }


}
